<?php

class models_setting
{

    private $_db;
    private $_cache;

    public function __construct($db, $cache)
    {
        $this->_db = $db;
        $this->_cache = $cache;
    }

    public function get($key)
    {
        $keys = $this->_db->select('setting', array('key_field' => $key), null, null, null, FALSE);
        $exce = $this->_db->getPDOException();
        if ($exce)
        {
            throw $exce;
        }
        return isset($keys[0]['value_field']) ? $keys[0]['value_field'] : null;
    }

    public function set($key, $value)
    {
        $s = $this->get($key);
        if (isset($s))
        {
            $result = $this->_db->queryFirst("
                UPDATE setting
                SET value_field = :value_field
                WHERE key_field = :key_field
                ", array(':key_field'=> $key, ':value_field'=> $value), FALSE);
        } else
        {
             $result = $this->_db->queryFirst("
                INSERT INTO setting ('id', key_field, value_field)
                VALUES (:id, :key, :value )
                ", array(':id' => '',':key'=> $key, ':value'=> $value), FALSE);
        }
    }

    public function clear($key)
    {
        if ($key)
        {
            return $this->_db->delete('setting', array('key_field' => $key));
        }
    }

}

?>
